/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_02_oop.inner_classes.Example_01_Inner_Class.java]
 * Creation Date 	[21-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_oop.inner_classes;

/**
 * 内部类
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_01_Inner_Class {

	private String name = "outer_class";
	private static int i = 10;

	static {
		System.out.println("outer_class static block");
	}

	{
		System.out.println("outer_class initial block");
	}

	/**
	 * 普通内部类
	 * 
	 * @author 不落的太阳(Sean Yang)
	 * @version 1.0
	 * @since JDK 6
	 */
	private class MyInnerClass {

		private String name = "inner_class";

		/**
		 * 此处的静态代码块是错误的,因为普通内部类相当于外部类中的一个属性(方法)
		 * 
		 * 内部类中的属性和方法相当于局部变量,因此不能使用static关键字
		 * 
		 * static { System.out.println("inner_class static block"); }
		 */
		{
			System.out.println("inner_class initial block");
		}

		public MyInnerClass() {
			System.out.println("inner_class constructor");
		}

		public void method2() {
			System.out.println("inner_class method2");
		}

		public void test() {
			System.out.println(name);
			System.out.println(this.name);
			System.out.println(Example_01_Inner_Class.this.name);// 调用外部类的同名属性
		}
	}

	/**
	 * 静态内部类
	 * 
	 * @author 不落的太阳(Sean Yang)
	 * @version 1.0
	 * @since JDK 6
	 */
	private static class MyStaticClass {

		static {
			System.out.println("static_class static block");
		}

		{
			System.out.println("static_class initial block");
		}

		public MyStaticClass() {
			System.out.println("static_class constructor");
		}

		public void method3() {
			Example_01_Inner_Class.i = 40;
			System.out.println(Example_01_Inner_Class.i);
		}
	}

	private Example_01_Inner_Class() {
		System.out.println("outer_class constructor");
	}

	/**
	 * method1
	 */
	public void method1() {
		System.out.println("outer_class method1");
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		Example_01_Inner_Class.MyInnerClass innerClass = new Example_01_Inner_Class().new MyInnerClass();
		innerClass.method2();
		innerClass.test();

		System.out.println();

		Example_01_Inner_Class.MyStaticClass staticInnerClass = new Example_01_Inner_Class.MyStaticClass();
		staticInnerClass.method3();
	}
}
